package com.yfp.operation.service.impl;

import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingReceiverV3Request;
import com.github.binarywang.wxpay.bean.profitsharing.result.ProfitSharingReceiverV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.ProfitSharingService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.yfp.operation.config.WxPayConfiguration;
import com.yfp.operation.config.WxPayProperties;
import com.yfp.operation.domain.PositionSharing;
import com.yfp.operation.service.PositionService;
import com.yfp.operation.service.WxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 微信业务
 *
 * @CreateDate: 2024/7/3 11:46
 * @UpdateUser:
 * @UpdateDate: 2024/7/3 11:46
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@Service
@Slf4j
public class WxServiceImpl implements WxService {
    @Autowired
    private WxPayProperties wxPayProperties;
    @Autowired
    private PositionService positionService;

    @Override
    public void addReceiver(List<PositionSharing> positionSharings) throws WxPayException {
        if (positionSharings.isEmpty()) {
            return;
        }
        WxPayProperties.Config wxConfig = wxPayProperties.getConfigs().get(1);
        WxPayService mxPayService = WxPayConfiguration.getMxPayService(wxConfig);
        ProfitSharingService profitSharingService = mxPayService.getProfitSharingService();
        ProfitSharingReceiverV3Request request = null;
        for (PositionSharing positionSharing : positionSharings) {
//            if (positionSharing.getType() == 4) {
//                continue;
//            }
            request = new ProfitSharingReceiverV3Request();
            request.setAppid(wxConfig.getAppId());
            request.setSubMchId(positionSharing.getProceedsAccount());
            request.setSubAppid(wxConfig.getAppId());
            request.setType(positionSharing.getType() == 3 ? "PERSONAL_OPENID" : "MERCHANT_ID");
            request.setAccount(positionSharing.getAccountNumber());
            request.setName(positionSharing.getType() != 4 ? positionSharing.getNickName() : "杭州约饭派科技有限公司");
            request.setRelationType(positionSharing.getType() != 4 ?"PARTNER" : "SERVICE_PROVIDER");
            ProfitSharingReceiverV3Result profitSharingReceiverV3Result = profitSharingService.addReceiverV3(request);
            log.info("添加分账关系，返回结果：{}", profitSharingReceiverV3Result);
        }
    }
}